{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "arbitrary-silicon",
   "metadata": {},
   "source": [
    "# Finnish Business Identifiers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "exciting-thailand",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "finite-animal",
   "metadata": {},
   "source": [
    "The function `clean_fi_ytunnus()` cleans a column containing Finnish business identifier (y-tunnus) strings, and standardizes them in a given format. The function `validate_fi_ytunnus()` validates either a single y-tunnus strings, a column of y-tunnus strings or a DataFrame of y-tunnus strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "perceived-brighton",
   "metadata": {},
   "source": [
    "y-tunnus strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"20774740\"\n",
    "* `standard`: y-tunnus strings with proper whitespace in the proper places, like \"2077474-0\"\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_fi_ytunnus()` and `validate_fi_ytunnus()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "charitable-chicken",
   "metadata": {},
   "source": [
    "### An example dataset containing y-tunnus strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "important-aquarium",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"ytunnus\": [\n",
    "            \"20774740\",\n",
    "            \"2077474-1\",\n",
    "            \"51824753556\",\n",
    "            \"51 824 753 556\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\"\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "competitive-street",
   "metadata": {},
   "source": [
    "## 1. Default `clean_fi_ytunnus`\n",
    "\n",
    "By default, `clean_fi_ytunnus` will clean ytunnus strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "narrow-recording",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_fi_ytunnus\n",
    "clean_fi_ytunnus(df, column = \"ytunnus\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "gothic-lecture",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "chief-exhaust",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "typical-winning",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "composed-maine",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fi_ytunnus(df, column = \"ytunnus\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "third-professor",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "hybrid-moral",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fi_ytunnus(df, column = \"ytunnus\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "angry-pacific",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned y-tunnus strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "alternate-cooking",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fi_ytunnus(df, column=\"ytunnus\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "moral-indianapolis",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "accomplished-customer",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "private-shareware",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fi_ytunnus(df, \"ytunnus\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "desperate-information",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "monthly-saturn",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fi_ytunnus(df, \"ytunnus\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "civic-taste",
   "metadata": {},
   "source": [
    "## 4. `validate_fi_ytunnus()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "unique-nightlife",
   "metadata": {},
   "source": [
    "`validate_fi_ytunnus()` returns `True` when the input is a valid y-tunnus. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_fi_ytunnus()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_fi_ytunnus()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_fi_ytunnus()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "accomplished-strip",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_fi_ytunnus\n",
    "print(validate_fi_ytunnus(\"20774740\"))\n",
    "print(validate_fi_ytunnus(\"2077474-1\"))\n",
    "print(validate_fi_ytunnus(\"51824753556\"))\n",
    "print(validate_fi_ytunnus(\"51 824 753 556\"))\n",
    "print(validate_fi_ytunnus(\"hello\"))\n",
    "print(validate_fi_ytunnus(np.nan))\n",
    "print(validate_fi_ytunnus(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "prepared-cricket",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "raising-norwegian",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_fi_ytunnus(df[\"ytunnus\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "appropriate-audio",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "conceptual-bridges",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_fi_ytunnus(df, column=\"ytunnus\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "velvet-timer",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "legislative-input",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "validate_fi_ytunnus(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "czech-ensemble",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
